package com.zzz.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

/*
 * @author zzr
 * @date: 2025/07/06  18:00
 * @description: HttpServletResponse 测试
 */
@WebServlet("/servlet10")
public class Servlet10 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置相应行相关的 API
        resp.setStatus(200);

        // 设置响应头的相关 API
        String info = "<h1>hello world</h1>";
        resp.setHeader("aaa","value"); // 如果 s 不是常规的响应头，就会在响应头中给我们创建一个这样的键值对
        resp.setHeader("Content-Type","text/html");
        resp.setContentType("text/html");
        resp.setHeader("Content-Length","1234");
        resp.setContentLength(info.getBytes().length);

        // 设置响应体内容的 API
        // 获得一个向响应体中输入文本字符输出类流
        PrintWriter writer = resp.getWriter();
        writer.write(info);
        // 获得一个向响应体中，输入二进制信息的字节输出流
        ServletOutputStream outputStream = resp.getOutputStream();
    }
}
